package com.example.lib_login.dataBinding

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.animation.BounceInterpolator
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import androidx.fragment.app.FragmentManager
import androidx.viewbinding.ViewBinding
import com.example.lib_login.LoginActivity
import com.example.lib_login.LoginViewModel
import com.example.lib_login.MainActivity
import com.example.lib_login.R
import com.example.lib_login.code.VerificationCodeFragment
import com.example.lib_login.databinding.AlertWindowViewBinding
import com.example.lib_login.databinding.FragmentPasswordBinding
import com.example.lib_login.view.InteractionManagement
import com.example.lib_login.view.WjbDialogView
import com.example.lib_ui_base.WjbAlertView
import java.util.Timer
import java.util.TimerTask
import java.util.logging.Handler

//事件
class Events {

    //改变协议的图片
    fun changeImageView(view: View,viewModel: LoginViewModel){
        val imageVIew = view as ImageView
        viewModel.displaySwitch = false
        viewModel.hiddenSwitch = false

        if(viewModel.agreementImageView.get()!!){//点亮需要取消
            viewModel.agreementImageView.set(false)
            imageVIew.setImageResource(R.drawable.img_1)
        }else{
            viewModel.agreementImageView.set(true)
            imageVIew.setImageResource(R.drawable.img)

        }

    }

    //登录
    fun login(view: View, viewModel: LoginViewModel, fragmentManager: FragmentManager){
        val textView = view as TextView
        val binding = AlertWindowViewBinding.inflate(LayoutInflater.from(view.context))


        if(viewModel.phoneNumber.get()!!.isEmpty()){
            //弹出提示
            binding.textView2.text  = "请输入手机号"

            WjbAlertView(view.context,binding.root).show()

            ObjectAnimator.ofFloat(textView,"translationX",0f,10f,0f,-10f,0f).apply {
                duration = 100
                repeatCount  = 3
                start()
            }

            return
        }

        //手机号不够11位
        if(viewModel.phoneNumber.get()!!.length < 11){

            //弹出提示
            binding.textView2.text  = "请输入11位数字的手机号"
            WjbAlertView(view.context,binding.root).show()

            ObjectAnimator.ofFloat(textView,"translationX",0f,10f,0f,-10f,0f).apply {
                duration = 100
                repeatCount  = 3
                start()
            }

            return
        }
        //没有勾选协议
        if(!viewModel.agreementImageView.get()!!){
            //弹出一个dialog  ChooseBottomFragment().show(supportFragmentManager,null)
            WjbDialogView(viewModel).show(fragmentManager,null)
            return
        }

        //满足登录的条件,进行发送验证码
        fragmentManager.beginTransaction().apply {
            val name = ViewCompat.getTransitionName(textView)
            if(name != null){
                addSharedElement(textView,"go")
            }
            replace(R.id.fragmentContainerView,VerificationCodeFragment())
            commit()
        }

    }

    //清空号码
    fun clear(view:View,viewModel: LoginViewModel){
        viewModel.phoneNumber.set("")
    }

    //开启定时器并发送验证码
    fun startTimer(view:View,viewModel: LoginViewModel){
        var time = viewModel.MY_TIME

        viewModel.userManager.requestSMSCode(viewModel.phoneNumber.get()!!,
            onEnd = {
                if (it) {
                    val mBinding = AlertWindowViewBinding.inflate(LayoutInflater.from(view.context))
                    mBinding.root.setBackgroundResource(R.drawable.success_alert_view)
                    mBinding.imageView.visibility = View.INVISIBLE
                    mBinding.textView2.setTextColor(Color.BLACK)
                    mBinding.textView2.text = "发送成功"
                    WjbAlertView(view.context,mBinding.root).show()
                } else {
                    val mBinding = AlertWindowViewBinding.inflate(LayoutInflater.from(view.context))
                    mBinding.textView2.text = "发送失败"
                    WjbAlertView(view.context,mBinding.root).show()
                }
            }
        )

        //发送验证码，开启定时器
        viewModel.timer = Timer()
        viewModel.timer.schedule(object : TimerTask(){
            override fun run() {
                time--
                if(time == 0){
                    viewModel.timer.cancel()
                    viewModel.time.set("重新获取")
                    time = viewModel.MY_TIME
                }else {
                    viewModel.time.set("$time")
                }
            }
        },0,1000)
    }


    //使用密码登录
    fun pwdLogin(view:View, viewModel: LoginViewModel, binding:ViewBinding,callback: InteractionManagement,activity:LoginActivity){
        val _binding = binding as FragmentPasswordBinding

        _binding.progressBar.visibility = View.VISIBLE

        _binding.progressBar.isEnabled = false
        callback.stop()



        //希望可以使用activity.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        //有两种方案
        val mBinding = AlertWindowViewBinding.inflate(LayoutInflater.from(view.context))

        //第一种方法：view.postDelayed在ui线程上执行
        view.postDelayed(
            {
                viewModel.userManager.loginByPhone(viewModel.phoneNumber.get()!!,_binding.pwdTextView.text.toString(),
                    onEnd = {
                        if(it){
                            activity.startActivity(Intent(activity,MainActivity::class.java))
                            activity.finish()
                        }else{
                            mBinding.textView2.text = "用户名或密码错误"
                            WjbAlertView(view.context,mBinding.root).show()
                        }
                    })
                _binding.progressBar.visibility = View.INVISIBLE
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
                //或者使用接口回调的callback.start()
            },2000
        )

        //第二种方法
       /* viewModel.timer = Timer()
        viewModel.timer.schedule(object :TimerTask(){
            override fun run() {
                _binding.progressBar.visibility = View.INVISIBLE
                activity.runOnUiThread {
                //或者使用接口回调的callback.start()
                    activity.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
                }
            }
        },1000)*/
    }







}